export default function (fn, delay, immediate = false) {
  let timer = null
  return function () {
    let args = [...arguments]
    return new Promise(resolve => {
      if (timer) clearTimeout(timer)

      if (immediate) {
        resolve(fn.apply(this,args))
        immediate = !immediate
      } else {
        timer = setTimeout(() => {
          resolve(fn.apply(this,args))
          immediate = !immediate
        },delay)
      } 
    })
  }
}